<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateFacilityTicketModelAPIRequest;
use App\Http\Requests\API\UpdateFacilityTicketModelAPIRequest;
use App\Models\FacilityTicketModel;
use App\Repositories\FacilityModelRepository;
use App\Http\Services\FacilityDTOBuilderService;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class FacilityTicketModelController
 * @package App\Http\Controllers\API
 */
class FacilityTicketController extends AppBaseController
{
    /** @var  FacilityTicketModelRepository */
    private $facilityTicketModelRepository;

//    public function __construct(FacilityTicketModelRepository $facilityTicketModelRepo)
//    {
//        $this->facilityTicketModelRepository = $facilityTicketModelRepo;
//    }

    /**
     * Display a listing of the FacilityTicketModel.
     * GET|HEAD /facilityTicketModels
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->facilityTicketModelRepository->pushCriteria(new RequestCriteria($request));
        $this->facilityTicketModelRepository->pushCriteria(new LimitOffsetCriteria($request));
        $facilityTicketModels = $this->facilityTicketModelRepository->all();

        return $this->sendResponse($facilityTicketModels->toArray(), 'Facility Ticket Models retrieved successfully');
    }

    /**
     * 查询用户的代金券
     * @param UserTicketRepository $repo
     * @return \Illuminate\Http\JsonResponse
     */
    public function search($id, FacilityModelRepository $repo, FacilityTicketModel $model)
    {
        $input = $this->request->all();

        $searchOptions = $this->normalizeSearchOptions();
        $sortOptions = $this->normalizeSortOptions();
        // 使用id 搜索
        $relations = $model->where('ticket_id', $id)->get()->toArray();
        if (empty($relations)) {
            return $this->success(['lists' => []]);
        }

        $faciltyList = array_column($relations, 'facility_id');
        $searchOptions['id'] = $faciltyList;

        $ret = $repo->search($searchOptions, $sortOptions);
        if (empty($ret['lists'])) {
            return $this->error(['lists' => []]);
        }

        $ret['lists'] = FacilityDTOBuilderService::buildList($ret['lists']);
        return $this->success($ret);
    }

    /**
     * Store a newly created FacilityTicketModel in storage.
     * POST /facilityTicketModels
     *
     * @param CreateFacilityTicketModelAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateFacilityTicketModelAPIRequest $request)
    {
        $input = $request->all();

        $facilityTicketModels = $this->facilityTicketModelRepository->create($input);

        return $this->sendResponse($facilityTicketModels->toArray(), 'Facility Ticket Model saved successfully');
    }

    /**
     * Display the specified FacilityTicketModel.
     * GET|HEAD /facilityTicketModels/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var FacilityTicketModel $facilityTicketModel */
        $facilityTicketModel = $this->facilityTicketModelRepository->findWithoutFail($id);

        if (empty($facilityTicketModel)) {
            return $this->sendError('Facility Ticket Model not found');
        }

        return $this->sendResponse($facilityTicketModel->toArray(), 'Facility Ticket Model retrieved successfully');
    }

    /**
     * Update the specified FacilityTicketModel in storage.
     * PUT/PATCH /facilityTicketModels/{id}
     *
     * @param  int $id
     * @param UpdateFacilityTicketModelAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateFacilityTicketModelAPIRequest $request)
    {
        $input = $request->all();

        /** @var FacilityTicketModel $facilityTicketModel */
        $facilityTicketModel = $this->facilityTicketModelRepository->findWithoutFail($id);

        if (empty($facilityTicketModel)) {
            return $this->sendError('Facility Ticket Model not found');
        }

        $facilityTicketModel = $this->facilityTicketModelRepository->update($input, $id);

        return $this->sendResponse($facilityTicketModel->toArray(), 'FacilityTicketModel updated successfully');
    }

    /**
     * Remove the specified FacilityTicketModel from storage.
     * DELETE /facilityTicketModels/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var FacilityTicketModel $facilityTicketModel */
        $facilityTicketModel = $this->facilityTicketModelRepository->findWithoutFail($id);

        if (empty($facilityTicketModel)) {
            return $this->sendError('Facility Ticket Model not found');
        }

        $facilityTicketModel->save();

        return $this->sendResponse($id, 'Facility Ticket Model deleted successfully');
    }
}
